#define BOOST_TEST_MAIN

#ifndef _MSC_VER

// Wyłączenie dynamicznego linkowania dla Windows
#define BOOST_TEST_DYN_LINK

#else

// Obejście problemów z deklaracjami typów w bibliotece jpeg pod
// Windows
#define XMD_H

#endif

#include <boost/test/included/unit_test.hpp>
#include <boost/filesystem.hpp>

#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/jpeg_io.hpp>

#include "../ged/Editor.hpp"
#include "../ged/Command.hpp"
#include "../ged/Observer.hpp"

using namespace ged;

BOOST_AUTO_TEST_SUITE( test_Command )

static const boost::filesystem::path image_path("tests/images/boost.jpg");

//FlipUpDownCommand
BOOST_AUTO_TEST_CASE( test_case_1 )
{
	Editor *editor = Editor::getInstance();
	PGILImage p1_image(new GILImage);

	jpeg_read_image(image_path.file_string(), *p1_image);
	editor->addImageLayer(p1_image);
	PCommand f1(new FlipUpDownCommand);
	editor->execute(f1);
	PImageLayer b = editor->getImage()->getLayer();

	editor->addImageLayer(p1_image);
	PImageLayer a = editor->getImage()->getLayer();
	a->setView(flipped_up_down_view (a->getView()));

	BOOST_CHECK(b->getView() == a->getView());

	editor->getImage()->selectLayer(0);
	PImageLayer c = editor->getImage()->getLayer();
	editor->unExecute();

	editor->getImage()->selectLayer(1);
	PImageLayer d = editor->getImage()->getLayer();
	d->setView(flipped_up_down_view (d->getView()));
	BOOST_CHECK(c->getView() == d->getView());

	editor->getImage()->deleteLayer();
	editor->getImage()->deleteLayer();
}
//FlipLeftRightCommand
BOOST_AUTO_TEST_CASE( test_case_2 )
{
	Editor *editor = Editor::getInstance();
	PGILImage p1_image(new GILImage);

	jpeg_read_image(image_path.file_string(), *p1_image);
	editor->addImageLayer(p1_image);
	PCommand f2(new FlipLeftRightCommand);
	editor->execute(f2);
	PImageLayer b = editor->getImage()->getLayer();

	editor->addImageLayer(p1_image);
	PImageLayer a = editor->getImage()->getLayer();
	a->setView(flipped_left_right_view (a->getView()));

	BOOST_CHECK(b->getView() == a->getView());

	editor->getImage()->selectLayer(0);
	PImageLayer c = editor->getImage()->getLayer();
	editor->unExecute();

	editor->getImage()->selectLayer(1);
	PImageLayer d = editor->getImage()->getLayer();
	d->setView(flipped_left_right_view (d->getView()));
	BOOST_CHECK(c->getView() == d->getView());

	editor->getImage()->deleteLayer();
	editor->getImage()->deleteLayer();
}
//SubimageCommand
BOOST_AUTO_TEST_CASE( test_case_3 )
{
	Editor *editor = Editor::getInstance();
	PGILImage p1_image(new GILImage);

	jpeg_read_image(image_path.file_string(), *p1_image);
	editor->addImageLayer(p1_image);
	//rozmiar orginalnego obrazka
	Image::view_type image_view =editor->getImage()->getView();

	PCommand s1(new SubimageCommand(0,0,50,50));
	editor->execute(s1);
	PImageLayer b = editor->getImage()->getLayer();

	editor->addImageLayer(p1_image);
	PImageLayer a = editor->getImage()->getLayer();
	a->setView(subimage_view(a->getView(),0, 0, 50, 50 ) );

	BOOST_CHECK(b->getView() == a->getView());

	editor->getImage()->selectLayer(0);
	PImageLayer c = editor->getImage()->getLayer();
	editor->unExecute();

	editor->getImage()->selectLayer(1);
	PImageLayer d = editor->getImage()->getLayer();
	d->setView(subimage_view(d->getView(),0,0,image_view.width(),image_view.height()));
	BOOST_CHECK(c->getView() == d->getView());

	editor->getImage()->deleteLayer();
	editor->getImage()->deleteLayer();
}
//SubsampleCommand
BOOST_AUTO_TEST_CASE( test_case_4 )
{
	Editor *editor = Editor::getInstance();

	PGILImage p1_image(new GILImage);

	jpeg_read_image(image_path.file_string(), *p1_image);
	editor->addImageLayer(p1_image);

	PCommand s2(new SubsampleCommand(2,3));
	editor->execute(s2);
	PImageLayer b = editor->getImage()->getLayer();

	editor->addImageLayer(p1_image);
	PImageLayer a = editor->getImage()->getLayer();
	a->setView(subsampled_view(a->getView(),2,3));

	BOOST_CHECK(b->getView() == a->getView());

	editor->getImage()->selectLayer(0);
	PImageLayer c = editor->getImage()->getLayer();
	editor->unExecute();

	editor->getImage()->deleteLayer();
	editor->getImage()->deleteLayer();
}
//RotateCwCommand
BOOST_AUTO_TEST_CASE( test_case_5 )
{
	Editor *editor = Editor::getInstance();
	PGILImage p1_image(new GILImage);

	jpeg_read_image(image_path.file_string(), *p1_image);
	editor->addImageLayer(p1_image);
	PCommand r1(new RotateCwCommand);
	editor->execute(r1);
	PImageLayer b = editor->getImage()->getLayer();

	editor->addImageLayer(p1_image);
	PImageLayer a = editor->getImage()->getLayer();
	a->setView(rotated90cw_view (a->getView()));

	BOOST_CHECK(b->getView() == a->getView());

	editor->getImage()->selectLayer(0);
	PImageLayer c = editor->getImage()->getLayer();
	editor->unExecute();

	editor->getImage()->selectLayer(1);
	PImageLayer d = editor->getImage()->getLayer();
	d->setView(rotated90ccw_view(d->getView()));

	BOOST_CHECK(c->getView() == d->getView());

	editor->getImage()->deleteLayer();
	editor->getImage()->deleteLayer();
}
//RotateCcwCommand
BOOST_AUTO_TEST_CASE( test_case_6 )
{
	Editor *editor = Editor::getInstance();
	PGILImage p1_image(new GILImage);

	jpeg_read_image(image_path.file_string(), *p1_image);
	editor->addImageLayer(p1_image);
	PCommand r2(new RotateCcwCommand);
	editor->execute(r2);
	PImageLayer b = editor->getImage()->getLayer();

	editor->addImageLayer(p1_image);
	PImageLayer a = editor->getImage()->getLayer();
	a->setView(rotated90ccw_view (a->getView()));

	BOOST_CHECK(b->getView() == a->getView());

	editor->getImage()->selectLayer(0);
	PImageLayer c = editor->getImage()->getLayer();
	editor->unExecute();

	editor->getImage()->selectLayer(1);
	PImageLayer d = editor->getImage()->getLayer();
	d->setView(rotated90cw_view(d->getView()));

	BOOST_CHECK(c->getView() == d->getView());

	editor->getImage()->deleteLayer();
	editor->getImage()->deleteLayer();
}

BOOST_AUTO_TEST_SUITE_END()
